home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 16 / Example 16.1 / avi.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-30  |  2.9 KB  |  161 lines

  1. #include "avi.h"        
  2.  
  3. AVI::AVI()
  4. {
  5.     AVIFileInit();
  6.     m_pCurrentFrame = NULL;
  7.  
  8.     m_timeMS = 0;
  9.     m_playing = m_done = false;
  10.     m_activeStream = 0;
  11.     m_lastFrame = -1;
  12.     m_pAviFile = NULL;
  13. }
  14.  
  15. AVI::~AVI()
  16. {
  17.     AVIFileExit();
  18.     Release();
  19. }
  20.  
  21. HRESULT AVI::Load(char fileName[], IDirect3DDevice9* Dev)
  22. {
  23.     try
  24.     {
  25.         m_pDevice = Dev;
  26.  
  27.         if(AVIFileOpen(&m_pAviFile, fileName, OF_READ, NULL) != 0)
  28.             return E_FAIL;
  29.  
  30.         //Retreive Audio streams
  31.         long streamNo = 0;
  32.         do
  33.         {
  34.             AUDIO_STREAM *as = new AUDIO_STREAM();
  35.  
  36.             //Get new m_pStream
  37.             if(AVIFileGetStream(m_pAviFile, &as->m_pStream, streamtypeAUDIO, streamNo++))
  38.             {
  39.                 delete as;
  40.                 break;
  41.             }
  42.  
  43.             //Add m_pStream
  44.             m_audio.push_back(as);
  45.         }
  46.         while(true);
  47.         
  48.         streamNo = 0;
  49.         do
  50.         {
  51.             VIDEO_STREAM *vs = new VIDEO_STREAM();
  52.  
  53.             //Get m_video m_pStream
  54.             if(AVIFileGetStream(m_pAviFile, &vs->m_pStream, streamtypeVIDEO, streamNo++))
  55.             {
  56.                 delete vs;
  57.                 break;
  58.             }
  59.  
  60.             //GetFrame object
  61.             vs->m_pGetFrame = AVIStreamGetFrameOpen(vs->m_pStream, NULL);
  62.  
  63.             //Get runtime
  64.             vs->m_runTime = AVIStreamEndTime(vs->m_pStream);
  65.  
  66.             //Add m_pStream
  67.             m_video.push_back(vs);
  68.         }
  69.         while(true);
  70.  
  71.         return S_OK;
  72.     }
  73.     catch(...)
  74.     {
  75.         debug.Print("Error in AVI::Load()");
  76.         return E_FAIL;
  77.     }
  78. }
  79.  
  80. void AVI::Release()
  81. {
  82.     for(int i=0;i<m_video.size();i++)
  83.         if(m_video[i] != NULL)
  84.             delete m_video[i];
  85.     m_video.clear();
  86.  
  87.     for(int i=0;i<m_audio.size();i++)
  88.         if(m_audio[i] != NULL)
  89.             delete m_audio[i];
  90.     m_audio.clear();
  91.  
  92.     if(m_pCurrentFrame != NULL)
  93.         m_pCurrentFrame->Release();
  94.     m_pCurrentFrame = NULL;
  95.  
  96.     AVIFileRelease(m_pAviFile);
  97. }
  98.  
  99. void AVI::Play()
  100. {
  101.     m_timeMS = 0;
  102.     m_playing = true;
  103.     m_done = false;
  104.     m_activeStream = 0;
  105.     m_lastFrame = -1;
  106. }
  107.  
  108. void AVI::Stop()
  109. {
  110.     m_playing = false;
  111. }
  112.  
  113. bool AVI::Done()
  114. {
  115.     return m_done;
  116. }
  117.  
  118. void AVI::Update(float deltaTime)
  119. {
  120.     if(m_playing)m_timeMS += deltaTime * 1000;
  121.     else return;
  122.  
  123.     long frame;
  124.     if(m_timeMS <= m_video[m_activeStream]->m_runTime)
  125.     {
  126.         frame = AVIStreamTimeToSample(m_video[m_activeStream]->m_pStream, m_timeMS);
  127.  
  128.         //Retrieve new frame
  129.         if(frame != m_lastFrame && frame != -1)
  130.         {
  131.             m_lastFrame = frame;
  132.  
  133.             BITMAPINFOHEADER *bip = NULL;
  134.             bip = (BITMAPINFOHEADER*)AVIStreamGetFrame(m_video[m_activeStream]->m_pGetFrame, frame);
  135.  
  136.             if(bip != NULL)
  137.             {
  138.                 if(m_pCurrentFrame != NULL)
  139.                     m_pCurrentFrame->Release();
  140.  
  141.                 D3DXCreateTextureFromFileInMemoryEx(m_pDevice, bip, bip->biSize + bip->biWidth * bip->biHeight * bip->biBitCount / 8,
  142.                                                     bip->biWidth, bip->biHeight,
  143.                                                     1, D3DUSAGE_DYNAMIC, D3DFMT_R8G8B8, D3DPOOL_DEFAULT,
  144.                                                     D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &m_pCurrentFrame);
  145.             }
  146.         }
  147.     }
  148.     else
  149.     {
  150.         m_activeStream++;
  151.         m_timeMS = 0;
  152.  
  153.         //No more streams to play
  154.         if(m_activeStream >= m_video.size())
  155.         {
  156.             Stop();
  157.             m_done = true;
  158.             m_activeStream = 0;
  159.         }
  160.     }    
  161. }